﻿using System;
using System.IO;

namespace DDDProject.Guidance.Utils.ExtensionMethods
{
    public static class DirectoryInfoExtensions
    {
        /// <summary>
        /// Copy directory structure recursively, from current directory to destination directory
        /// </summary>
        /// <param name="source">Current source directory</param>
        /// <param name="dest">Destination directory</param>
        static public void Copy(
            this DirectoryInfo source,
            DirectoryInfo dest)
        {
            FileSystemInfo[] elements;

            if (!dest.Exists)
                dest.Create();

            elements = source.GetFileSystemInfos();
            foreach (FileSystemInfo element in elements)
            {
                string destPath = dest.FullName.EndsWith(Path.DirectorySeparatorChar.ToString()) ? dest.FullName : dest.FullName + Path.DirectorySeparatorChar;

                if (element is DirectoryInfo)
                { // Sub directories

                    (element as DirectoryInfo).Copy(new DirectoryInfo(destPath + element.Name));
                }
                else
                { // Files in directory

                    File.Copy(element.FullName, destPath + element.Name, true);
                }
            }
        }
    }
}
